package com.timu.kafka.day1;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @描述:kafk第一个例子，
 * @Author: zhangh
 * @Time: 2021/3/29 15:52
 **/

/**
 * 1.先在服务器上创建一个topic"mytopic"
 * bin/kafka-topics.sh --create --topic mytopic --bootstrap-server localhost:9092
 *
 * 2.运行消费脚本
 * bin/kafka-console-consumer.sh --topic mytopic --from-beginning --bootstrap-server localhost:9092
 * 成功打印出结果
 *
 *
 */
public class Demo1 {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "139.155.10.40:9092");
        props.put("acks", "1");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<String, String>("mytopic", Integer.toString(i), Integer.toString(i)));
        }

        producer.close();
    }
}
